﻿using System;
using Loki.UI.Win.Resources;
using Loki.Utils;

namespace Loki.UI.Win
{
    public class EnumStringValueConverter<T> : LokiObject, IValueConverter
    {
        public T DefaultValue { get; private set; }

        public EnumStringValueConverter(T P_DefaultValue = default(T))
        {
            DefaultValue = P_DefaultValue;
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(T))
            {
                throw BuildErrorFormat<ArgumentException>(ErrorMessages.TUI_020, targetType, typeof(T));
            }

            if (value == null)
            {
                return DefaultValue;
            }
            else
            {
                return (T)Enum.Parse(targetType, value.ToString());
            }
        }
    }
}
